package codegen;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import codegen.util.DbUtil;
import codegen.util.FileUtil;
import codegen.util.database.Table;

public class CodeGen {

	private static final String configFile = System.getProperty("user.dir")
			+ File.separator + "config" + File.separator + "config.properties";
	private static String genPath = System.getProperty("user.dir")
			+ File.separator + "gen" + File.separator;
	

	private static Map<String, Table> tableMap = new HashMap<String, Table>();
	private static List<Table> allTables = new ArrayList<Table>();
	private static Connection conn = null;

	private static void initParameter(String dbName,String pacakgeName, String pacakgePreFix) throws FileNotFoundException,
			IOException {
		Properties properties = new Properties();
		properties.load(new FileInputStream(configFile));
		CommonPara.package_prefix = /*"cn.fl."+*/pacakgePreFix+pacakgeName.toLowerCase();
		CommonPara.package_name = pacakgeName.toLowerCase();
		CommonPara.dbSchema = dbName.toUpperCase();
		CommonPara.dbDriver = properties.getProperty("dbDriver");
		//CommonPara.dbUrl = properties.getProperty("dbUrl")+dbName;//MYSQL
		CommonPara.dbUrl = properties.getProperty("dbUrl");//ORACLE
		CommonPara.dbUser = properties.getProperty("dbUser");
		CommonPara.dbPassword = properties.getProperty("dbPassword");
	}

	public static Table getTable(String tableName) {
		Table table = null;

		if (tableMap.get(tableName) != null) {
			table = tableMap.get(tableName);
		} else {
			try {
				for (int i = 0; i < allTables.size(); i++) {
					Table t = allTables.get(i);
					if (tableName.equals(t.getTableName())) {
						table = t;
						table.setTableSeq(i);
						DbUtil.readFieldListFromDB(conn, CommonPara.dbSchema,
								CommonPara.dbDriver, table, i);
						break;
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			tableMap.put(tableName, table);
		}
		return table;
	}

	private static void genCode() throws Exception {
		CodeGenEngine engine = new CodeGenEngine(genPath);
		try {
			conn = DbUtil.getConnection();
			allTables = DbUtil.getAllTables(conn);
			System.out.println("allTables=" + allTables.size());
			FileUtil.delDirectory(new File(genPath));
			for (int i = 0; i < allTables.size(); i++) {
				Table table = allTables.get(i);
				table.setTableSeq(i);
				System.out.println(i + "--table_name=" + table.getTableName());
				if (tableMap.get(table.getTableName()) != null) {
					table = tableMap.get(table.getTableName());
				} else {
					DbUtil.readFieldListFromDB(conn, CommonPara.dbSchema,
							CommonPara.dbDriver, table, i);
					tableMap.put(table.getTableName(), table);
				}
				engine.setTable(table);
				engine.genCode();
			}
			System.out.println("生成完成");
		} catch (Exception ex) {
			throw ex;
		} finally {
			if (conn != null)
				try {
					conn.close();
					conn = null;
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
		}
	}

	public static void main(String[] args) throws Exception {
		//第一个参数为命名空间名字(大写)   第二个参数为模块名称（小写）   第三个参数包前缀
		initParameter("PL_TEST_FUJINGRUN","auccar", "fl.seccar.service.");
		genCode();
	}

}
